﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using RouterControl.Whs.SingletonService;

namespace RouterControl.WhsAddIn.ConsoleControls
{
    public partial class RouterOverview : ConsoleControlBase
    {
        private RootDeviceInfo _device;

        public RouterOverview(RouterControlConsoleTab2 tab)
			: base(tab)
		{
			if (_log.IsDebugEnabled)
				_log.Debug("Initialization start");

			try
			{
				InitializeComponent();
	
				this.lineBox.Header.Text = Properties.Resources.txtSelectedRouter;

                this.selectedRouterName.Text = this.selectedRouterDesc.Text =
                    this.selectedRouterModel.Text = this.selectedRouterUrl.Text =
                    Properties.Resources.txtLoading;

				// done
				if (_log.IsDebugEnabled)
					_log.Debug("Initialization done");
			}
			catch (Exception e)
			{
				_log.Fatal("Failed to initialize", e);

				throw;
			}
		}

        private void openSettingsLink_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            _tab.OpenRouterControlSettings();
        }

        private void RouterOverview_Load(object sender, EventArgs e)
        {
            if (_log.IsDebugEnabled)
                _log.Debug("Loading start");

            // device selected?
            if (!_singletonService.Channel.IsSelectedDeviceFound())
            {
                _tab.ResetState();

                return;
            }

            // continue
            try
            {
	            {
	                var dev = _singletonService.Channel.GetSelectedDevice();
	
	                if (!dev.HasValue)
	                    throw new Exception("no device selected");
	
	                _device = dev.Value;
	            }
	
	            // setup information
	            this.selectedRouterName.Text = _device.FriendlyName;
	            this.selectedRouterDesc.Text = _device.Description;
	            this.selectedRouterModel.Text = _device.ModelName;
	
	            if (string.IsNullOrEmpty(_device.PresentationURL))
	            {
	                this.selectedRouterUrl.Text = Properties.Resources.txtUnsupported;
	            }
	            else
	            {
	                this.selectedRouterUrl.Text = _device.PresentationURL;
	                this.selectedRouterUrl.Enabled = true;
	            }

                // done
                if (_log.IsDebugEnabled)
                    _log.Debug("Loading done");
            }
            catch (Exception ex)
            {
                _log.Fatal("Loading failed", ex);

                throw;
            }
        }

        private void selectedRouterUrl_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            _tab.Services.OpenUrl(_device.PresentationURL);
        }
    }
}
